//
//  YSSearchProdListCtrl.m
//  Yueshi
//
//  Created by yueshi－mac1 on 16/2/23.
//  Copyright © 2016年 悦食中国. All rights reserved.
//

#import "YSSearchProdListCtrl.h"
#import "YSProductListGridItemCell.h"
#import "YSProductDetailCtrl.h"
#import "YSCategoryDataTool.h"

@interface YSSearchProdListCtrl ()<UICollectionViewDataSource, UICollectionViewDelegate>
@property(nonatomic,strong) UICollectionView *searchCollectionView;
@property(nonatomic,assign) NSInteger pageIndex;
@property(nonatomic,strong) NSMutableArray *prodArray;
@property(nonatomic,copy) NSString *keywords;
@end

@implementation YSSearchProdListCtrl
static NSString * const productReuseIdentifier = @"YSProductListItemCell";

- (instancetype)initWithData:(NSArray *)array keywords:(NSString *)keywords{
    self = [super init];
    if (self) {
        [self.prodArray addObjectsFromArray:array];
        self.keywords = keywords;
    }
    return self;
}
- (NSMutableArray *)prodArray{
    if(!_prodArray){
        _prodArray =  [NSMutableArray array];
    }
    return _prodArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = self.keywords;
    
    [self initSearchCollectionView];
    [self requestData];
}

-(void)initSearchCollectionView{
    UICollectionViewFlowLayout *flowLayout =[[UICollectionViewFlowLayout alloc]init];
    [flowLayout setMinimumLineSpacing:5];
    [flowLayout setMinimumInteritemSpacing:5];
    
    UICollectionView *collectionView  =[[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, SCREENW, SCREENH - NavWithStatusBarH) collectionViewLayout:flowLayout];
    [collectionView setBackgroundColor:HEXColor(@"#eaeae9")];
    [collectionView registerClass:[YSProductListGridItemCell class] forCellWithReuseIdentifier:productReuseIdentifier];
    
    [collectionView setDelegate:self];
    [collectionView setDataSource:self];
    [collectionView setAlwaysBounceVertical:YES];
    [collectionView registerNib:[UINib nibWithNibName:productReuseIdentifier bundle: [NSBundle mainBundle]] forCellWithReuseIdentifier:productReuseIdentifier];
    [self.view addSubview:collectionView];
    MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(searchHeaderRefresh)];
    header.lastUpdatedTimeLabel.hidden = YES;
    collectionView.header = header;
    collectionView.footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(searchFooterRefresh)];
    self.searchCollectionView = collectionView;
}

-(void)searchHeaderRefresh{
    self.pageIndex = 0;
    [self requestData];
}

-(void)searchFooterRefresh{
    self.pageIndex ++;
    [self requestData];
}

- (void)requestData{
    [YSCategoryDataTool searchDataWithKeywords:self.keywords searchType:1 pageIndex:self.pageIndex success:^(YSSearchRes *result) {
        [self searchCollectionViewComplete];
        if (result.isSucc) {
            if (_pageIndex == 0) {
                [self.prodArray removeAllObjects];
            }
            [self.prodArray addObjectsFromArray:result.productList];
            [self.searchCollectionView reloadData];
        }
    } failure:^(NSError *error) {
        [self searchCollectionViewComplete];
        ShowTips(kRequestFailure);
    }];
}

-(void)searchCollectionViewComplete{
    if(_searchCollectionView){
        [_searchCollectionView.header endRefreshing];
        [_searchCollectionView.footer endRefreshing];
    }
}

#pragma mark <UICollectionViewDataSource>
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.prodArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    YSProductListGridItemCell *cell = [YSProductListGridItemCell cellWithCollectionView:collectionView indexPath:indexPath];
    [cell setProd:self.prodArray[indexPath.row]];
    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    return CGSizeMake((SCREENW-25)/2, 250);
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    NSInteger prodId = [self.prodArray[indexPath.row] prodId];
    YSProductDetailCtrl *ctrl =  [[YSProductDetailCtrl alloc]initWithProdId:prodId];
    [self.navigationController pushViewController:ctrl animated:YES];
}

-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    return UIEdgeInsetsMake(10,10,10,10);
}
@end
